package cc.eddic.practice.concurrency.impl;

import cc.eddic.practice.concurrency.LogItem;
import cc.eddic.practice.concurrency.LogItemParser;
import cc.eddic.practice.concurrency.LogParser;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class LogParserLxImpl implements LogParser {
    private LogItemParser logParser=new LogItemParseLxImpl();

    @Override
    public Map<String, List<LogItem>> parse(String folderName) {
        Map<String,List<LogItem>> map=new HashMap<>();
        List<File> fileList=new ArrayList<>();
        fileResult(new File(folderName),fileList);
        fileList.stream().filter(file -> file.getName().endsWith(".log"))
                .forEach(f->{
                    List<LogItem> logItemList=readList(f.getPath()).stream()
                            .map(fileString->logParser.parse(fileString)).filter(Optional::isPresent)
                            .map(Optional::get).collect(Collectors.toList());
                    map.put(f.getName(),logItemList);
                });
        if(log.isInfoEnabled()) log.info("logMap is {}",map.size());
        int count=0;int validNum=0;
        for(String s:map.keySet()){
            count+=map.get(s).size();
            validNum+=map.get(s).stream().filter(LogItem::isValidRequest).count();
        }            if(log.isInfoEnabled()) log.info("logMapCOunt is {},shai is {}",count,validNum);
        return map;
    }
    private void fileResult(File file,List<File> fileList){
        val files=file.listFiles();
        if(files==null){
            return;
        }
        for(val f:files){
            if(f.isDirectory()){
                fileResult(f,fileList);
            }else fileList.add(f);
        }
    }
//    读文件的每一行内容
    private Stream<String> read(String textFileName){
        try{
            val p=Paths.get(textFileName);
            return Files.lines(p);
        }catch (IOException e){
            if (log.isDebugEnabled()) log.debug("{}", textFileName);
            return null;
        }
    }
//    把读的内容放进list里
    private List<String> readList(String textFileName){
        return read(textFileName).collect(Collectors.toList());
    }

//    复杂版
//    public Map<String, List<LogItem>> parse(String folderName) {
//        File folder = new File(folderName);
//        File[] files = folder.listFiles();
//        Map<String, List<LogItem>> listMap=new HashMap<>();
//            for (File file : files) {
//                if (file.isFile()) {
////                    if(log.isInfoEnabled()) log.info("文件 is {}",file.getPath());
//                    List<LogItem> logItemList=new ArrayList<>();
//                    if(file.getName().substring(file.getName().lastIndexOf(".")).equals(".log")){
//                        try {
//                            List<String> allLines = Files.readAllLines(Paths.get(file.getPath()));
//                            for (String line : allLines) {
//                                if(logParser.parse(line).isPresent()){
//                                    LogItem logItem=logParser.parse(line).get();
//                                    logItemList.add(logItem);
//                                }
//                            }
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
////                        if(log.isInfoEnabled()) log.info("logite is {}",logItemList.stream().filter(logItem -> logItem.isValidRequest()).count());
//                        listMap.put(file.getPath(),logItemList);
//                    }
//                }else if(file.isDirectory()){
//                    Map<String, List<LogItem>>newMap= parse(file.getPath());
//                    for(String s:newMap.keySet()){
//                        listMap.put(s,newMap.get(s));
//                    }
//                }
//            }
////        if(log.isInfoEnabled()) log.info("logMap is {}",listMap.size());
//        int count=0;int validNum=0;
//        for(String s:listMap.keySet()){
//            count+=listMap.get(s).size();
//            validNum+=listMap.get(s).stream().filter(LogItem::isValidRequest).count();
//        }            if(log.isInfoEnabled()) log.info("logMapCOunt is {},shai is {}",count,validNum);
//
//        return listMap;
//    }




}
